﻿/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
ListNode* findMeet(ListNode* head) {
    ListNode* fast = head;
    ListNode* slow = head;
    while (fast && fast->next) {
        fast = fast->next->next;
        slow = slow->next;
        if (slow == fast) {
            return slow;
        }
    }
    return NULL;
}
struct ListNode* detectCycle(struct ListNode* head) {
    ListNode* meet = findMeet(head);
    if (meet == NULL) {
        return NULL;
    }
    ListNode* pcur = head;
    while (pcur != meet) {
        pcur = pcur->next;
        meet = meet->next;
    }
    return meet;
}